﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using log4net;
using ADC.DAL;
using ADC.BL;

namespace ADC.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            log4net.Config.XmlConfigurator.Configure();
            Application["VisitorsAreOnline"] = 0L;

            //GET USER_SESSION_COUNT AND USER_PAGE_VIEW_COUNT FROM DATABASE
            ApplicationObject appObject = new ApplicationObject();
            appObject = ApplicationObjectFactory.getByName(ApplicationObjectFactory.USER_SESSION_COUNT);

            //if USER_SESSION_COUNT is not exist, create new one
            if (appObject == null)
            {
                appObject = new ApplicationObject();
                appObject.ApplicationObjectName = ApplicationObjectFactory.USER_SESSION_COUNT;
                appObject.ApplicationObjectValueLong = 0L;
                ApplicationObjectFactory.Insert(appObject);
            }

            //Get MAX_USER_ONLINE from database
            ApplicationObject appObjectMaxUserOnline = new ApplicationObject();
            appObjectMaxUserOnline = ApplicationObjectFactory.getByName(ApplicationObjectFactory.MAX_USER_ONLINE);
            //if MAX_USER_ONLINE is not exist, create new one
            if (appObjectMaxUserOnline == null)
            {
                appObjectMaxUserOnline = new ApplicationObject();
                appObjectMaxUserOnline.ApplicationObjectName = ApplicationObjectFactory.MAX_USER_ONLINE;
                appObjectMaxUserOnline.ApplicationObjectValueLong = 0L;
                appObjectMaxUserOnline.ApplicationObjectValueDatetime = DateTime.Now;
                ApplicationObjectFactory.Insert(appObjectMaxUserOnline);
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();

            long visitorsAreOnline = (long)Application["VisitorsAreOnline"];
            Application["VisitorsAreOnline"] = visitorsAreOnline + 1;
            visitorsAreOnline = visitorsAreOnline + 1;

            //update maximum user are online (MAX_USER_ONLINE)
            //Get MAX_USER_ONLINE from database
            ApplicationObject appObjectMaxUserOnline = new ApplicationObject();
            appObjectMaxUserOnline = ApplicationObjectFactory.getByName(ApplicationObjectFactory.MAX_USER_ONLINE);
            if (visitorsAreOnline == appObjectMaxUserOnline.ApplicationObjectValueLong)
            {
                appObjectMaxUserOnline.ApplicationObjectValueDatetime = DateTime.Now;
                ApplicationObjectFactory.Update(appObjectMaxUserOnline);
            }
            else if (visitorsAreOnline > appObjectMaxUserOnline.ApplicationObjectValueLong)
            {
                appObjectMaxUserOnline.ApplicationObjectValueLong = visitorsAreOnline;
                appObjectMaxUserOnline.ApplicationObjectValueDatetime = DateTime.Now;
                ApplicationObjectFactory.Update(appObjectMaxUserOnline);
            }

            Application.UnLock();

            // update USER_SESSION_COUNT
            ApplicationObject appObject = new ApplicationObject();
            appObject = ApplicationObjectFactory.getByName(ApplicationObjectFactory.USER_SESSION_COUNT);
            if (appObject != null)
            {
                appObject.ApplicationObjectValueLong = appObject.ApplicationObjectValueLong + 1;
                ApplicationObjectFactory.Update(appObject);
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            //Application.Lock();

            //long visitorsAreOnline = (long)Application["VisitorsAreOnline"];
            //Application["VisitorsAreOnline"] = visitorsAreOnline - 1;

            //Application.UnLock();

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}